package com.example.selfservicestation.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.navigation.fragment.findNavController
import com.example.selfservicestation.R
import com.example.selfservicestation.base.BaseBindingFragment
import com.example.selfservicestation.constant.DEFAULT_LOGIN_CARD
import com.example.selfservicestation.constant.SP_LOCK_COUNT
import com.example.selfservicestation.constant.SP_LOGIN_CARD
import com.example.selfservicestation.databinding.FragmentHomeBinding
import com.example.selfservicestation.extensions.safeNavigate
import com.example.selfservicestation.extensions.serialNo
import com.example.selfservicestation.mob.MBFrame
import com.example.selfservicestation.mob.MODBUS_MIN_SEND_INTERVAL
import com.example.selfservicestation.mob.ModBusController
import com.example.selfservicestation.mob.ScannerManager
import com.example.selfservicestation.singleton.SharedPreferenceUtil
import com.example.selfservicestation.util.LOG_TAG
import com.example.selfservicestation.util.toHexString
import com.example.x6.gpioctl.GpioUtils

class HomeFragment: BaseBindingFragment<FragmentHomeBinding>(FragmentHomeBinding::inflate) {

    lateinit var gpioUtils: GpioUtils
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        gpioUtils = GpioUtils.getInstance("/dev/rk_gpio")
        initViews()
//        binding.btnGetPocketByPhone.setOnClickListener {
////            gpioUtils.setGpioDirection(123, GpioUtils.GPIO_DIRECTION_OUT)
////            gpioUtils.gpioSetValue(123,GpioUtils.GPIO_VALUE_LOW)
////            gpioUtils.setGpioDirection(123, GpioUtils.GPIO_DIRECTION_IN)
////            Toast.makeText(context,"${gpioUtils?.gpioGetValue(123)}",Toast.LENGTH_SHORT).show()
//
//            ModBusController.modBusManager?.sendTo(0, MBFrame(0),true, MODBUS_MIN_SEND_INTERVAL){
//                Log.d("MainActivity",it.toHexString(true))
//            }
//        }
    }

    @SuppressLint("SetTextI18n")
    private fun initViews(){
//        binding.tvDeviceNo.text = "设备编号：${context?.serialNo()}"
        binding.btnGetPocketByPhone.setOnClickListener {
            findNavController().safeNavigate(R.id.homeFragment_to_phoneFragment)
        }

//        binding.ivLogo.setOnLongClickListener {
//            findNavController().safeNavigate(R.id.homeFragment_to_configMenuFragment)
//            true
//        }
        checkCount()
    }

    private fun checkCount(){
        ModBusController.modBusManager?.sendTo(0, MBFrame(lockIndex = -2), false, MODBUS_MIN_SEND_INTERVAL) { resData ->
            Log.d("HomeFragment", resData.toHexString(true))
            if (resData.isEmpty()) return@sendTo
            SharedPreferenceUtil.putInt(SP_LOCK_COUNT,resData[5].toInt())
        }
    }

    override fun onResume() {
        super.onResume()
    }
}